package com.zhangyue.wqercs.patterndemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private final List<ItemBean> list;
    private Context context;
    public MyAdapter(Context context,List<ItemBean> list){
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.grid_item,viewGroup,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int i) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),list.get(i).getImgId());
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(@Nullable Palette palette) {
                Palette.Swatch dominantSwatch = palette.getDominantSwatch();
                int rgb = dominantSwatch.getRgb();
                int bodyTextColor = dominantSwatch.getBodyTextColor();
                int titleTextColor = dominantSwatch.getTitleTextColor();
                myViewHolder.itemView.setBackgroundColor(rgb);
                myViewHolder.tv.setTextColor(bodyTextColor);
            }
        });
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)myViewHolder.itemView.getLayoutParams();
        myViewHolder.iv.setImageBitmap(bitmap);
        myViewHolder.tv.setText(list.get(i).getItem());

        Log.e("aaa","iv height:"+myViewHolder.iv.getLayoutParams().height+" tv height:"+myViewHolder.tv.getLayoutParams().height);
        layoutParams.height = myViewHolder.iv.getLayoutParams().height+myViewHolder.tv.getLayoutParams().height;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tv;
        ImageView iv;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.tv = itemView.findViewById(R.id.tv);
            this.iv = itemView.findViewById(R.id.iv);
        }
    }
}
